package com.dzt.androidframework.jsoup.news.model;

import com.dzt.androidframework.jsoup.news.APi;
import com.dzt.androidframework.jsoup.news.bean.ImageNews;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by M02323 on 2017/7/12.
 */

public class RemoteNewsDataSource implements NewsDataSource {

	private RemoteNewsDataSource() {
	}

	private static class SingletonHolder {
		static RemoteNewsDataSource sInstance = new RemoteNewsDataSource();
	}

	public static RemoteNewsDataSource getInstance() {
		return RemoteNewsDataSource.SingletonHolder.sInstance;
	}

	public void init() {
	}

	@Override
	public void getImageNewList(int page, final OnImageNewLoadListener listener) {
		OkHttpUtils
				.get()
				.url(APi.IMAGE_NEW_LIST_URL + "page=" + page)
				.build()
				.execute(new StringCallback() {
					@Override
					public void onError(Call call, Exception e) {
						listener.onFailed(e.getMessage(), e);
					}

					@Override
					public void onResponse(String response) {
						try {
							String json = response.split("\\(")[1].split("\\)")[0];
							ImageNews imageNews = new Gson().fromJson(json, ImageNews.class);
							listener.onSuccess(imageNews.getData());
						}
						catch (Exception e){

						}
					}
				});
	}
}
